function Game(el){
    this.el = document.querySelector(el)

    // 创建地图
    this.map = new Map(this.el)
    this.map.create()
    // 创建食物
    this.food = new Food(this.el)
    this.food.create()
    //创建蛇
    this.snake = new Snake(this.el)
    this.snake.create()
    //计时器数据
    this.t=null
}

Game.prototype.start = function(){
    this.t=setInterval(()=>{
        this.snake.move()
        // 判断吃
        let snakeX = this.snake.snakeObj[0].x*20
        let snakeY = this.snake.snakeObj[0].y*20
        let foodX = this.food.foodX
        let foodY = this.food.foodY

        if(snakeX==foodX && snakeY==foodY ){
            let last = this.snake.snakeObj[this.snake.snakeObj.length - 1]
            // alert('aa')
            this.snake.snakeObj.push({
                x: last.x, 
                y : last.y, 
                color: last.color, 
                el:null
            })
            // 重新加载蛇
            this.snake.create() 
            // 随机食物
            this.food.move()
        }

    },150)
    document.addEventListener('keydown',(evt)=>{
        e = evt || window.event
        switch (e.keyCode) {
            case 38:
            if (this.snake.direction == 'bottom') {
                alert('游戏结束')
            }
                this.snake.direction = 'top'
                break;
            case 40:
            if (this.snake.direction == 'top') {
                alert('游戏结束')
            }
                this.snake.direction = 'bottom'
                break;
            case 37:
            if (this.snake.direction == 'right') {
                alert('游戏结束')
            }
                this.snake.direction = 'left'
                break;
            case 39:
            if (this.snake.direction == 'left') {
                alert('游戏结束')
            }
                this.snake.direction = 'right'
                break;
            default:
                break;
        }
    })
}

Game.prototype.stop = function(){
    clearInterval(this.t)
        alert('游戏结束')
}